import errorEvent from '../../errorMessage';
import store from '@/store/index'
import httpService from '../../httpService';


let formatApiData = api => {
    api.id = api.apiId;
    api.apiFolderId = api.folderId;
    api.param = JSON.parse(api.param.replace(/\&apos\;/g, "'"));
    api.bodyType = api.param.bodyType;
    api.header = api.param.header;
    api.body = api.param.body;
    api.fromdata = api.param.fromdata;
    api.query = api.param.query;
    api.rest = api.param.rest;
    api.before = api.param.before;
    api.after = api.param.after;
    api.response = api.param.response;
    if (api.response === undefined) {
        api.response = JSON.stringify({
            key: '',
            type: 'object',
            model: {},
            annotate: '',
            children: [{
                key: '',
                type: '',
                model: {},
                annotate: '',
                children: [],
            }],
        })
    }
    return api;
};


export default {
    async getRenderData() {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'get',
                url: 'http://' + serviceAddress + '/getRenderData',
                params: {
                    projectId: projectId,
                }
            });
            let folderList = data.folders;
            let apiList = data.apis;
            for (let i = 0; i < folderList.length; i++) {
                const folderItem = folderList[i];
                folderItem.id = folderItem.folderId;
            };
            for (let i = 0; i < apiList.length; i++) {
                const api = apiList[i];
                formatApiData(api);
            };
            return {
                apiList: apiList,
                folderList: folderList,
            };
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async creatFolder(newFolder, parentFolder) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        try {
            await httpService({
                type: 'post',
                url: 'http://' + serviceAddress + '/folder',
                headers: { token: store.state.mainOptions.token },
                params: { projectId: store.state.mainOptions.project.projectId, },
                data: {
                    projectId: projectId,
                    parentFolderId: parentFolder.folderId,
                    title: newFolder.title,
                    description: newFolder.description,
                    parentFolderIndexArray: parentFolder.newFolderArray,
                }
            });
            return newFolder;
        } catch (error) {
            errorEvent(error);
            return false;
        }

    },
    async updataFolderDetail(updateObj) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            await httpService({
                type: 'put',
                url: 'http://' + serviceAddress + '/folder',
                headers: { token: token },
                params: { projectId: projectId, },
                data: {
                    folderId: updateObj.id,
                    title: updateObj.title,
                    description: updateObj.description,
                }
            });
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    async updataFolderOrderByInto(updateObj) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            await httpService({
                type: 'put',
                url: 'http://' + serviceAddress + '/folder/order/into',
                headers: { token: token },
                params: { projectId: projectId, },
                data: {
                    fromFolder: updateObj.fromFolder,
                    moveFolder: updateObj.moveFolder,
                    toFolder: updateObj.toFolder,
                }
            });
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    async updateFolderOrderBySame(updateObj) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            await httpService({
                type: 'put',
                url: 'http://' + serviceAddress + '/folder/order/same',
                headers: { token: token },
                params: { projectId: projectId, },
                data: {
                    index: updateObj.index,
                    id: updateObj.id,
                }
            });
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    async deleteFolder(param) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            await httpService({
                type: 'delete',
                url: 'http://' + serviceAddress + '/folder',
                headers: { token: token },
                params: {
                    projectId: projectId,
                    folderId: param.folderId,
                    parentId: param.parentId,
                }
            });
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    async creatApi(param) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            var data = await httpService({
                type: 'post',
                url: 'http://' + serviceAddress + '/api',
                headers: { token: token },
                params: { projectId: projectId, },
                data: {
                    projectId: projectId,
                    folderId: param.folderId,
                    title: param.title,
                    type: param.type,
                    status: param.status,
                    description: param.description,
                    url: param.url,
                    param: param.param,
                    oldApiArray: param.oldApiArray,
                }
            });
            return data.apiId;
        } catch (error) {
            errorEvent(error);
            return false;
        }

    },
    async updataApi(param) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            await httpService({
                type: 'put',
                url: 'http://' + serviceAddress + '/api',
                headers: { token: token },
                params: { projectId: projectId, },
                data: {
                    id: param.id,
                    title: param.title,
                    type: param.type,
                    status: param.status,
                    description: param.description,
                    url: param.url,
                    param: param.param,
                }
            });
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        }
    },
    async updataApiOrderBySame(param) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            await httpService({
                type: 'put',
                url: 'http://' + serviceAddress + '/api/order/same',
                headers: { token: token },
                params: { projectId: projectId, },
                data: {
                    id: param.id,
                    index: param.index,
                }
            });
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async updataApiOrderByDifferent(param) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            await httpService({
                type: 'put',
                url: 'http://' + serviceAddress + '/api/order/different',
                headers: { token: token },
                params: { projectId: projectId, },
                data: {
                    from: param.from,
                    to: param.to,
                    id: param.id,
                }
            });
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async deleteApi(id) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        var token = store.state.mainOptions.token;
        try {
            await httpService({
                type: 'delete',
                url: 'http://' + serviceAddress + '/api',
                headers: { token: token },
                params: {
                    id: id,
                    projectId: projectId,
                }
            });
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        };

    },
    async searchApi(str, type) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;

        try {
            var apiList = await httpService({
                type: 'get',
                url: 'http://' + serviceAddress + '/api',
                params: {
                    type: type,
                    str: str,
                    projectId: projectId,
                }
            });
            for (let i = 0; i < apiList.length; i++) {
                const api = apiList[i];
                formatApiData(api);
            };
            return apiList;
        } catch (error) {
            errorEvent(error);
            return false;
        };

    },
}